package com.autoEcole.service;

import java.util.List;
import java.util.Map;

import com.autoEcole.dao.DatePermisDAO;
import com.autoEcole.dao.EleveDAO;
import com.autoEcole.dao.EtatDAO;
import com.autoEcole.dao.PermisDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.DatePermis;
import com.autoEcole.model.Eleve;
import com.autoEcole.model.Etat;
import com.autoEcole.model.Permis;

public class DatePermisService {
	
	private DatePermisDAO datePermisDAO;
	private EleveDAO eleveDAO;
	private PermisDAO permisDAO;
	private EtatDAO etatDAO;
	/**
	 * DatePermisService
	 */
	private static DatePermisService datePermisService;
	
	/**
	 * Constructor
	 */
	private DatePermisService() {
		super();
	}

	/**
	 * Returns the DatePermisService instance (Design Pattern : Singleton)
	 * 
	 * @return DatePermisService
	 */
	public static synchronized DatePermisService getInstance() {
		if (datePermisService == null)
			datePermisService = new DatePermisService();
		return datePermisService;
	}

	public DatePermisDAO getDatePermisDAO() {
		return datePermisDAO;
	}

	public void setDatePermisDAO(DatePermisDAO datePermisDAO) {
		this.datePermisDAO = datePermisDAO;
	}

	public EleveDAO getEleveDAO() {
		return eleveDAO;
	}

	public void setEleveDAO(EleveDAO eleveDAO) {
		this.eleveDAO = eleveDAO;
	}

	public PermisDAO getPermisDAO() {
		return permisDAO;
	}

	public void setPermisDAO(PermisDAO permisDAO) {
		this.permisDAO = permisDAO;
	}

	public EtatDAO getEtatDAO() {
		return etatDAO;
	}

	public void setEtatDAO(EtatDAO etatDAO) {
		this.etatDAO = etatDAO;
	}

	public void create(DatePermis datePermis) throws ApplicationException {
		try {
			datePermisDAO.create(datePermis);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(DatePermis datePermis) throws ApplicationException {
		try {
			datePermisDAO.update(datePermis);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public List<DatePermis> findByEleveId(int eleveId) throws ApplicationException {
		try {
			Map<Integer, Eleve> mapEleve = eleveDAO.findAllMap();
			Map<Integer, Etat> mapEtat = etatDAO.findAllMap();
			Map<Integer, Permis> mapPermis = permisDAO.findAllMap();
			List<DatePermis> listDatePermis = datePermisDAO.findByEleveId(eleveId);
			for (DatePermis datePermis : listDatePermis) {
				datePermis.setEleve(mapEleve.get(eleveId));
				int etatId = datePermis.getEtat().getEtatId();
				datePermis.setEtat(mapEtat.get(etatId));
				int permisId = datePermis.getPermis().getPermisId();
				datePermis.setPermis(mapPermis.get(permisId));
			}
			return listDatePermis;
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(DatePermis datePermis) throws ApplicationException {
		try {
			datePermisDAO.delete(datePermis);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
